<!--
* description: 整车销售---未配车订单客户查询
* author:xlc
* createdDate: 2020-12-11
-->
<template>
  <div class="app-container app-container-table">
    <div class="oatable">
      <one-table-template
        ref="multipleTable"
        :dynamic-buttons="tableButtons"
        :dynamic-components="tableComponents"
        :dynamic-api-config="apiConfig"
        :dynamic-table-cols="tableCols"
        :dynamic-form-fields="formField"
        :dynamic-is-show-select="true"
        :dynamic-is-show-more-btn="true"
        :callback-obj="callbackObj"
      />
    </div>
    <Details
      v-if="detPopupsVisible"
      :key="editPopupsKey"
      :detpopups-visible="detPopupsVisible"
      :data-for-form="editRowData"
      :popups-state="editPopupsState"
      :title-name="titleName"
      @closedet="closedet"
    />
    <chadan-auto
      :key="switchPopupsKey"
      :popups-visible="switchPopupsVisible"
      @close="autoClose"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import { orgApis } from '@/api/vesalesapi.js'
import OneTableTemplate from '@/components/templates/oneTable'
import Details from '../orderAllocation/details'
import Vue from 'vue'
import chadanAuto from './chadanAuto'
import commData from '../common/index.js'
import { exportExcel } from '../../../../utils/getTimeString'
export default {
  name: 'OrderAllocation',
  components: {
    OneTableTemplate,
    chadanAuto,
    Details
  },
  mixins: [oneTableWithViewTemplateMixins],
  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, 'OrderAllocation-Details'], () => {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      titleName: '',
      editRowData: {},
      switchPopupsKey: '',
      editPopupsState: '',
      detPopupsVisible: false,
      selectList: [],
      // 网格查询API配置对象
      apiConfig: orgApis.orderNoCarQuery,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          name: 'search', // 按钮图标样式
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          type: '',
          size: 'small',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          name: 'reset',
          position: 'right' // 重置
        },
        {
          compKey: 'btnKey3',
          type: '',
          size: 'small',
          disabled: true,
          parentFileds: 'disabled:table',
          clickEvent: () => this.chadan(),
          text: '插单', // 新增
          name: 'add',
          position: 'left'
        },
        {
          compKey: 'btnKey4',
          type: '',
          size: 'small',
          clickEvent: () => { this.exportExcelNo() },
          text: '导出', // 新增
          name: 'export',
          position: 'left'
        },
        {
          compKey: 'btnKey5',
          type: '',
          size: 'small',
          clickEvent: () => this.autoshow(),
          text: '自动插单',
          name: 'export',
          position: 'left'
        }
      ],
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            // 显示组件
            {
              compKey: 'compKey1',
              labelName: 'DMS订单编号：',
              codeField: 'saleOrderCode',
              isMul: false,
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: 'C端订单编号：',
              codeField: 'saleOrderCodeC',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey18',
              labelName: '销售计划号：',
              codeField: 'salePlanCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey12',
              labelName: '下单日期：',
              codeField: 'reserveDateStartTime,reserveDateEndTime',
              component: () => import('@/components/org/datePicker/datePicker.vue'),
              dateType: 'daterange',
              isMust: true
            },
            // {
            //   compKey: 'compKey19',
            //   labelName: 'VIN码：',
            //   codeField: 'vin',
            //   component: () => import('@/components/org/commonInput'),
            //   type: 'inputText',
            //   isMul: false,
            //   isMust: true
            // },
            {
              compKey: 'compKey4',
              labelName: '客户姓名：',
              codeField: 'custName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey5',
              labelName: '客户电话：',
              codeField: 'custTel',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey6',
              labelName: '证件号码：',
              codeField: 'custCardNo',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey7',
              labelName: '购买方式：',
              codeField: 'buyTypeId',
              lookuptype: 'VE0117',
              component: () => import('@/components/org/LookupValue'),
              type: 'dropdownList',
              isMust: false
            },
            {
              compKey: 'compKey9',
              labelName: '交付门店：',
              codeField: 'deliveryDlrId',
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMust: false,
              oFields: 'deliveryDlrId,deliveryDlrShortName',
              customObj: {
                api: orgApis.ListOfDeliveryStore
              }
            },
            {
              compKey: 'compKey10',
              labelName: '交付专员：',
              codeField: 'empId',
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMust: false,
              parentFileds: 'value:deliveryDlrId',
              oFields: 'empId,empName',
              customObj: {
                listQuery: { deliveryDlrId: '' },
                api: orgApis.ListOfdeliveryPersonnel
              }
            },
            // {
            //   compKey: 'compKey11',
            //   labelName: '订单标签：',
            //   codeField: 'saleOrderSign',
            //   lookuptype: 'VE1013',
            //   component: () => import('@/components/org/LookupValue'),
            //   type: 'dropdownList',
            //   isMust: false
            // },
            ...commData.ModelOptions()
          ],
      // 动态生成网格列
      tableCols: [
        {
          prop: 'controlBtn',
          label: '操作', // 操作
          codeField: 'orgName',
          width: null,
          fixed: true,
          align: 'center',
          isComponent: true,
          comps: [
            {
              compKey: 'propKey1',
              align: 'center',
              labelName: '详情',
              codeField: 'orgName',
              clickEvent: index => { this.details(index) },
              component: () => import('@/components/org/linkButton')
            }
          ]
        },
        {
          prop: 'saleOrderId',
          label: '',
          hidden: true
        },
        {
          prop: 'updateControlId',
          label: '',
          hidden: true
        }, {
          prop: 'salesPlanId',
          label: '',
          hidden: true
        }, {
          prop: 'saleOrderId',
          label: '',
          hidden: true
        }, {
          prop: 'saleOrderId',
          label: '',
          hidden: true
        },
        {
          prop: 'saleOrderCode',
          label: 'DMS订单编号',
          width: 120
        },
        {
          prop: 'saleOrderCodeC',
          label: 'C端订单编号',
          width: 120
        },
        {
          prop: 'insertOrderStatus',
          label: '插单状态',
          width: 120
        },
        {
          prop: 'orderStatusName',
          label: '订单状态',
          width: 120
        },
        {
          prop: 'salePlanCode',
          label: '销售计划号',
          width: 120
        },
        {
          prop: 'vin',
          label: 'VIN码',
          width: 160,
          hidden: true
        },
        {
          prop: 'SaleChannels',
          label: '订单来源',
          width: 120
        },
        {
          prop: 'saleOrderSignName',
          label: '订单标签',
          width: 120
        },
        {
          prop: 'receivedAmount',
          label: '已收金额',
          width: 120
        },
        {
          prop: 'advicePrice',
          label: '整车金额',
          width: 120
        },
        {
          prop: 'custName',
          label: '客户姓名',
          width: 120
        },
        {
          prop: 'custTel',
          label: '客户电话',
          width: 120
        },
        {
          prop: 'custCardName',
          label: '证件类型',
          width: 120
        },
        {
          prop: 'custCardNo',
          label: '证件号码',
          width: 160
        },
        {
          prop: 'custLxName',
          label: '联系人姓名',
          width: 120
        },
        {
          prop: 'custLxPhone',
          label: '联系人电话',
          width: 120
        },
        {
          prop: 'buyTypeName',
          label: '购买方式',
          width: 120
        },
        {
          prop: 'buyUserName',
          label: '购买用途',
          width: 120
        },
        {
          prop: 'buyModleName',
          label: '购买形态',
          width: 120
        },
        {
          prop: 'reserveDate',
          label: '下单日期',
          width: 180
        },
        {
          prop: 'caEmpName',
          label: '销售人员',
          width: 120
        },
        {
          prop: 'caEmpPhone',
          label: '销售人员电话',
          width: 120
        },
        {
          prop: 'crmPreDeliverDate',
          label: '预计交付日期',
          width: 180
        },
        {
          prop: 'dlrShortName',
          label: '交付门店',
          width: 120
        },
        {
          prop: 'storeCategory',
          label: '门店类别',
          width: 120
        },
        {
          prop: 'empName',
          label: '交付专员',
          width: 120
        },
        // {
        //   prop: 'sendPersionDate',
        //   label: '车辆发运日期',
        //   width: 180
        // },
        // {
        //   prop: 'deliveryDate',
        //   label: '实际交付日期',
        //   width: 180
        // },
        // {
        //   prop: 'pdiStatusName',
        //   label: '新车PI状态',
        //   width: 120
        // },
        // {
        //   prop: 'pdiDate',
        //   label: '新车PDI时间',
        //   width: 180
        // },
        {
          prop: 'carSeriesCode',
          label: '车系编码',
          width: 120
        },
        {
          prop: 'carSeriesCn',
          label: '车系名称',
          width: 120
        },
        {
          prop: 'CAR_COLOR_CODE',
          label: '基础车型编码',
          width: 120
        },
        {
          prop: 'carConfigCn',
          label: '基础车型名称',
          width: 120
        },
        {
          prop: 'optionalPagName',
          label: '选装包',
          width: 120
        },
        {
          prop: 'carColorName',
          label: '车身颜色',
          width: 120
        },
        {
          prop: 'carIncolorName',
          label: '内饰颜色',
          width: 120
        },
        {
          prop: 'remark',
          label: '备注',
          width: 120
        }
      ],
      formField: {
        saleOrderCode: '',
        saleOrderCodeC: '',
        salePlanCode: '',
        // vin: '',
        custName: '',
        custTel: '',
        custCardNo: '',
        buyTypeId: '',
        deliveryDlrId: '',
        empId: '',
        // saleOrderSign: '',
        reserveDateStartTime: '',
        reserveDateEndTime: '',
        carSeriesId: '',
        carConfigId: '',
        optionalPagId: '',
        carColorId: '',
        carIncolorId: ''
      },
      // 是否使用刷新Key的方式刷新弹窗
      resetDialogKey: true,
      switchPopupsVisible: false,
      selectiondata: [],
      // 单表格模版页面组件回调事件
      callbackObj: {
        onCheckSelectChange: (selection) => {
          this.selectiondata = selection
          this.selectiondata = selection.reduce((pre, cur) => {
            pre.push({
              saleOrderId: cur.saleOrderId,
              updateControlId: cur.updateControlId,
              UPDATE_CONTROL_ID: cur.UPDATE_CONTROL_ID,
              carSerId: cur.carSerId,
              carConfigId: cur.carConfigId,
              CAR_CONFIG_CODE: cur.CAR_CONFIG_CODE,
              carOptionalpagId: cur.carOptionalpagId,
              OPTIONAL_PAG_CODE: cur.OPTIONAL_PAG_CODE,
              carColorId: cur.carColorId,
              CAR_COLOR_CODE: cur.CAR_COLOR_CODE,
              carIncolorId: cur.carIncolorId,
              CAR_INCOLOR_CODE: cur.CAR_INCOLOR_CODE,
              ttCode: cur.ttCode,
              vin: cur.vin,
              saleOrderCode: cur.saleOrderCode,
              saleOrderCodeC: cur.saleOrderCodeC,
              insertOrderStatuss: cur.insertOrderStatuss
            })
            return pre
          }, [])
          this.onCheckSelectChange1(selection)
        }
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.queryTable(1)
    })
  },
  methods: {
    // 自动插单
    autoshow() {
      this.switchPopupsVisible = true
    },
    // 选择列是否生效
    // 插单
    chadan() {
      this.$refs.multipleTable.listLoading = true
      const that = this
      const queryObj = {
        // api配置
        apiConfig: orgApis.orderNoCarInsert,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          // 当前中台使用的名称有dataInfo、info，具体查看API文档
          dataInfo: { list: this.selectiondata }
        }
      }
      // 转换了中台请求格式数据
      var paramD = that.$getParams(queryObj)
      // 调用中台API方法（可复用）
      that.$requestAPI(paramD).then(response => {
        this.$message({
          message: response.msg,
          duration: 2000
        })
        if (response.result === '1') {
          this.$refs.multipleTable.listLoading = false
          this.queryTable(1)
        }
      })
    },
    // 导出
    exportExcelNo() {
      exportExcel(this.formField, orgApis.orderNoCarExport.APIUrl, orgApis.orderMatchCarExport.ServiceCode, '未配车订单明细.xlsx')
    },
    // 选择列是否生效
    getSelectable(row, index) {
      return (row.insertOrderStatuss !== '2')
    },
    onCheckSelectChange1(selection) {
      var btns = this.tableButtons[2]
      const isCheck = selection && selection.length > 0
      btns.disabled = !isCheck
    },
    closedet() {
      this.detPopupsVisible = false
    },
    details(index) {
      this.detPopupsVisible = true
      this.editRowData = this.$refs.multipleTable.list[index]
    },
    autoClose(type) {
      this.switchPopupsVisible = false
      if (type === '1') {
        this.queryTable()
      }
    }

  }
}
</script>
<style lang="scss" scoped>
// /deep/ .el-table__header .el-checkbox{
//     display:none
//   }
</style>
